package com.wrial.offer;
/*
 * @Author  Wrial
 * @Date Created in 15:55 2020/3/31
 * @Description 合并有序链表
 */

public class FaceTest_25 {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {

        if (l1 == null) return l2;
        if (l2 == null) return l1;
        ListNode ans = new ListNode(-1);
        ListNode pAns = ans;
        ListNode p1 = l1.next;
        ListNode p2 = l2.next;
        while (l1 != null && l2 != null) {

            if (l1.val < l2.val) {
                pAns.next = l1;
                l1 = l1.next;
            } else {
                pAns.next = l2;
                l2 = l2.next;
            }
            pAns = pAns.next;
        }
        if (l1 != null) {
            pAns.next = l1;

        }
        if (l2 != null) {
            pAns.next = l2;
        }
        return ans.next;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
